package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo2 {
    public static void main(String[] args) {
        try {
            // 正确加载 MySQL 驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            String url = "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
            String username = "root";
            String password = "ROOT";

            Connection conn = DriverManager.getConnection(url, username, password);

            // 输出连接成功信息
            System.out.println("数据库连接成功！");

            Statement statement = conn.createStatement();
            String sql = "INSERT INTO user (name, password, email, phone, address, regTime) " +
                    "VALUES ('张三', '123456', 'zhangsan@example.com', '12345678901', '北京市海淀区中关村', NOW())";
            int rows = statement.executeUpdate(sql);
            if (rows > 0) {
                System.out.println("插入成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
